package util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

import com.concerneyes.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import common.PreferUtil;

/**
 * Created by Administrator on 2017/7/7 0007.
 */

public class Util {
    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 判断qq是否可用
     *
     * @param context
     * @return
     */
    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentTime_Today() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new java.util.Date());
    }

    /**
     *  String日期格式转换
     * @param date
     * @return
     */
    public static String changeStringDataStyle(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
        long time = 0L;
        try {
            time = sdf.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(time);
    }
    /**
     *  String日期格式转换为指定格式
     * @param date
     * @return
     */
    public static String changeStringDataStyle(String date,String dataStyle) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
        long time = 0L;
        try {
            time = sdf.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sf = new SimpleDateFormat(dataStyle);
        return sf.format(time);
    }
    public static void setTriangle(Context context,TextView textView) {
        Drawable right = context.getResources().getDrawable(R.drawable.down_arrow);
        right.setBounds(0, 0, ActivityUtil.dip2px(8), ActivityUtil.dip2px(8));
        textView.setCompoundDrawables(null, null, right, null);
        textView.setCompoundDrawablePadding(ActivityUtil.dip2px(4));
    }
    public static String getVersionName(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            String version = packInfo.versionName;
            return version;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "1.0";
    }
    public static boolean isFirst(Context context){
        String version = PreferUtil.getVersion();
        if (version == null || !version.equals(Util.getVersionName(context))) {
            return  true;
        }else {
            return  false;
        }
    }
}
